//------------------------------------------------------------------------------
//SFWK Framework
//Copyright (C) 2011 SQLI

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

using System;
using SFWK.Core;

namespace SFWK.Core.TraceAdapter
{
    /// <summary>
    /// Provide Framework Logging tool access, adapted(wrapped) to SFWK framework logging interface
    /// </summary>
    public class Log4NetTraceAdapter : ILogger
    {
        //private readonly log4net.ILog _log4NetLogger;

        private static log4net.ILog _traceSource;
        private static log4net.ILog _tracer
        {
            get
            {
                if (_traceSource == null)
                {
                    log4net.Config.XmlConfigurator.Configure();
                    _traceSource = log4net.LogManager.GetLogger("Log4NetManager");                    
                }
                return _traceSource;
            }
        }


        #region ILogger Members

        /// <summary>
        /// Logging method
        /// </summary>
        /// <param name="source"></param>
        /// <param name="title"></param>
        /// <param name="message"></param>
        /// <param name="severity"></param>
        public void Log(object source, string title, string message, ELogSeverity severity)
        {
            string logMsg = String.Format("{0}: {1}: {2}",
                                source == null ? "null" : source.GetType().ToString(),
                                title, message);
            switch (severity)
            {
                case ELogSeverity.Error:        {_tracer.Error(logMsg); return;}
                case ELogSeverity.Information:  {_tracer.Info(logMsg); return;}
                case ELogSeverity.Debug:        {_tracer.Debug(logMsg);return;}
                case ELogSeverity.Critical:     {_tracer.Fatal(logMsg); return;}
                case ELogSeverity.Warning:      {_tracer.Warn(logMsg); return;}
            }
        }

        /// <summary>
        /// Logging method
        /// </summary>
        /// <param name="source"></param>
        /// <param name="title"></param>
        /// <param name="exception"></param>
        /// <param name="severity"></param>
        public void Log(object source, string title, Exception exception, ELogSeverity severity)
        {
            string logMsg = String.Format("{0}: {1}",
                                source == null ? "null" : source.GetType().ToString(), title);
            switch (severity)
            {
                case ELogSeverity.Error: { _tracer.Error(logMsg, exception); return; }
                case ELogSeverity.Information: { _tracer.Info(logMsg, exception); return; }
                case ELogSeverity.Debug: { _tracer.Debug(logMsg, exception); return; }
                case ELogSeverity.Critical: { _tracer.Fatal(logMsg, exception); return; }
                case ELogSeverity.Warning: { _tracer.Warn(logMsg, exception); return; }
            }
        }



        public void Log(object source, string title, string message, Exception exception, ELogSeverity severity)
        {
            string logMsg = String.Format("{0}: {1} \\r\\n{2}",
                                source == null ? "null" : source.GetType().ToString(), title, message);
            switch (severity)
            {
                case ELogSeverity.Error: { _tracer.Error(logMsg, exception); return; }
                case ELogSeverity.Information: { _tracer.Info(logMsg, exception); return; }
                case ELogSeverity.Debug: { _tracer.Debug(logMsg, exception); return; }
                case ELogSeverity.Critical: { _tracer.Fatal(logMsg, exception); return; }
                case ELogSeverity.Warning: { _tracer.Warn(logMsg, exception); return; }
            }
        }

        #endregion
    }
}
